// pages/activity/loverActivity/loverActivity.js
const app = getApp()
import { requestApi } from '../../../api'
import { formatImage } from '../../../utils/formatImage'
import { formatPrice } from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityData: {},
    shareImage: '',
    channel: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'

    this.data.channel = channel
    this.getActiveData()
  },

  getActiveData() {
    wx.showLoading({
      title: '加载中...',
    })

    app.request({
      url: requestApi.loverActivityUrl,
      login: false
    }, (res) => {
      wx.hideLoading()
      let activityData = res.original.data

      if (activityData.top_pic) {
        activityData.top_pic.new_path = activityData.top_pic.path ? formatImage.cdnImage(activityData.top_pic.path, formatImage.THUMBNAIL_L) : ''
      }

      if (activityData.topic_resource && activityData.topic_resource.length) {
        activityData.topic_resource.forEach((topicItem) => {
          topicItem.banner_image.new_path = topicItem.banner_image.path ? formatImage.cdnImage(topicItem.banner_image.path, formatImage.THUMBNAIL_L) : ''
          topicItem.collection_url = '../activityDetail/activityDetail?tId=' + topicItem.tid + '&channel=' + this.data.channel
        })
      }

      if (activityData.goods_list && activityData.goods_list.length) {
        activityData.goods_list.forEach((goodsItem) => {
          goodsItem.title_image.new_path = goodsItem.title_image.path ? formatImage.cdnImage(goodsItem.title_image.path) : ''
          goodsItem.title_image.padding_bottom = (goodsItem.title_image.height / goodsItem.title_image.width * 100) + '%'
          if (goodsItem.header_image) {
            goodsItem.header_image.new_path = goodsItem.header_image.path ? formatImage.cdnImage(goodsItem.header_image.path, formatImage.THUMBNAIL_L) : ''
            goodsItem.header_image.padding_bottom = (goodsItem.header_image.height / goodsItem.header_image.width * 100) + '%'
          }

          if (goodsItem.data && goodsItem.data.length) {
            goodsItem.data.forEach((dataItem) => {
              dataItem.goods_detail_url = '../../goods/goodsDetail/goodsDetail?gid=' + dataItem.gid + '&channel=' + this.data.channel
              dataItem.product_cover_image.new_path = dataItem.product_cover_image.path ? formatImage.cdnImage(dataItem.product_cover_image.path, formatImage.THUMBNAIL_L) : ''
              dataItem.product_cover_image.padding_bottom = (dataItem.product_cover_image.height / dataItem.product_cover_image.width * 100) + '%'
              dataItem.new_original_price = dataItem.original_price ? '￥' + formatPrice(dataItem.original_price / 100) : ''
              dataItem.new_goods_price = dataItem.goods_price ? '￥' + formatPrice(dataItem.goods_price / 100) : ''
            })
          }
        }) 
      }

      if (activityData.collection_list) {
        activityData.collection_list.bottom_img.new_path = activityData.collection_list.bottom_img.path ? formatImage.cdnImage(activityData.collection_list.bottom_img.path) : ''
        activityData.collection_list.bottom_img.padding_bottom = (activityData.collection_list.bottom_img.height / activityData.collection_list.bottom_img.width * 100) + '%'

        activityData.collection_list.top_img.new_path = activityData.collection_list.top_img.path ? formatImage.cdnImage(activityData.collection_list.top_img.path) : ''
        activityData.collection_list.top_img.padding_bottom = (activityData.collection_list.top_img.height / activityData.collection_list.top_img.width * 100) + '%'

        if (activityData.collection_list.data_list && activityData.collection_list.data_list.length) {
          activityData.collection_list.data_list.forEach((listItem) => {
            listItem.background_image.new_path = listItem.background_image.path ? formatImage.cdnImage(listItem.background_image.path, formatImage.THUMBNAIL_L) : ''
            listItem.background_image.padding_bottom = (listItem.background_image.height / listItem.background_image.width * 100) + '%'
            if (listItem.data && listItem.data.length) {
              listItem.data.forEach((dataItem) => {
                dataItem.collection_url = '../activityDetail/activityDetail?tId=' + dataItem.tid + '&channel=' + this.data.channel
                if (dataItem.banner_image) {
                  dataItem.banner_image.new_path = dataItem.banner_image.path ? formatImage.cdnImage(dataItem.banner_image.path, formatImage.THUMBNAIL_L) : ''
                  
                }
              })
            }
          })
        }
      }
      this.setData({
        activityData: activityData,
        shareImage: activityData.top_pic.new_path,
      })
    }, error => {
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '精选好礼 约“惠”七夕',
      path: '/pages/activity/loverActivity/loverActivity?channel=wx_xcx',
      imageUrl: this.data.shareImage
    }
  }
})